package com.k2.whereareyou;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.k2.whereareyou.engine.Engine;
import com.k2.whereareyou.interfaces.IConstants;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainMenuActivity extends Activity implements IConstants, View.OnClickListener{

	private Button[] buttons;
	private int language;
	private String timeStamp;
	private boolean type;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        language = Engine.setLanguage(getApplicationContext());
        initLayout();
        try {
			initUI();
		} catch (IOException e) {
		}
        checkError();
    }
	

	private void checkError() {
		try{
			if (getIntent().getExtras().getBoolean("error")){
				Engine.toast(getApplicationContext(), TEXT_PHOTO_DOWNLOAD[language][8]);
			}
		}catch (Exception e) {
		}
	}


	private void initUI() throws IOException{
    	buttons = new Button[BUTTONS_NUMBER_MAIN_MENU.length];
    	Log.d("add_buttons", String.valueOf(buttons.length));
    	for (int i=0; i < BUTTONS_NUMBER_MAIN_MENU.length; i++){
    		buttons[i] = (Button) findViewById(BUTTONS_NUMBER_MAIN_MENU[i]);
    		buttons[i].setText(TEXT_MAINMENU[language][i]);
    		buttons[i].setOnClickListener(this);
    	}
	}	
	
	private void initLayout() {
		setContentView(R.layout.activity_main_menu);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(Menu.NONE, IDM_PREF_CHANGE, Menu.NONE, Engine.TEXT_MENU_SETTINGS[language][0]);
    	menu.add(Menu.NONE, IDM_PREF_EXIT, Menu.NONE, Engine.TEXT_MENU_SETTINGS[language][1]);
    	menu.add(Menu.NONE, IDM_PREF_CLOSE, Menu.NONE, Engine.TEXT_MENU_SETTINGS[language][2]);
        return (super.onCreateOptionsMenu(menu));
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case IDM_PREF_CHANGE:
            	startActivity(new Intent(this, ChangeInfoActivity.class));
    	    	System.exit(0);
                break;
            case IDM_PREF_EXIT:
            	SharedPreferences prefs = getSharedPreferences("PreferensLogin", 0);
            	SharedPreferences.Editor editor = prefs.edit();
            	editor.putString("pass", "");
            	editor.commit();
            	startActivity(new Intent(this, MainActivity.class));
    	    	System.exit(0);
                break;
            case IDM_PREF_CLOSE:
            	System.exit(0);
                break;
            default:
                return false;
        }
        return true;
    }
	
	@Override
	public void onClick(View activity) {
		switch (activity.getId()) {
	    case R.id.button_foto:
	    	type = true; //type photo
	    	Intent intentPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	    	createPath(type);
	    	intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(
	    			new File(timeStamp)));
	    	intentPhoto.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 10);
	    	startActivityForResult(intentPhoto, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	      break;
	    case R.id.button_video:
	    	type = false;
	    	Intent intentVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
	    	createPath(type);
	    	Log.d("path", "timeStamp");
	    	intentVideo.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(
	    			new File(timeStamp)));
	    	intentVideo.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
	    	intentVideo.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 30);
	    	startActivityForResult(intentVideo, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
	      break;
	    default:
	      break;
	    }
	}
		
	private void createPath(boolean type) {
		File sdPath = Environment.getExternalStorageDirectory();
		sdPath = new File(sdPath.getAbsolutePath() + "/Where_are_you");
		sdPath.mkdirs();
		timeStamp = Environment.getExternalStorageDirectory() + "/Where_are_you/IMG_"+ 
    			new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) ;
		SharedPreferences prefs = getSharedPreferences("PreferensLogin", 0);
		SharedPreferences.Editor editor = prefs.edit();
		if (type){
			timeStamp = timeStamp + ".jpg";
			editor.putBoolean("type", type);
		}
		else{
			timeStamp = timeStamp + ".mp4";
			editor.putBoolean("type", type);
		}
    	editor.putString("timeStamp", timeStamp);
    	editor.commit();
		
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
		
		super.onActivityResult(requestCode, resultCode, data);
			SharedPreferences prefs = getSharedPreferences("PreferensLogin", 0);
			timeStamp = prefs.getString("timeStamp", "");
			Log.d("reqest", timeStamp);
			File file = new File(timeStamp);
			if (file.exists() && file.isFile()){
				Log.d("reqest", "file");
				Intent intentFoto = new Intent(this, PhotoDownloadActivity.class);			
				intentFoto.putExtra("path", prefs.getString("timeStamp", ""));
				intentFoto.putExtra("type", prefs.getBoolean("type", true));
				startActivity(intentFoto);
				System.exit(0);
		}
	}
	
	@Override
	public void onBackPressed() {
		System.exit(0);
	}
	
}
